package com.liangwenb.flutter_forbidshot;

import android.app.Activity;
import android.content.Context;

import android.util.Log;
import android.view.WindowManager;
import android.media.AudioManager;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;


import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

/**
 * FlutterForbidshotPlugin
 */
public class FlutterForbidshotPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {
    private MethodChannel channel;
    private Activity activity;


    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
        channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_forbidshot");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);

    }

    @Override
    public void onMethodCall(MethodCall call, Result result) {
        if(activity==null){
            return;
        }
        if (call.method.equals("setOn")) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
            setSecure(true);
        } else if (call.method.equals("setOff")) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
            setSecure(false);
        }
    }

    public void setSecure(boolean isSecure) {
        if(activity==null){
            Log.d("FlutterForbidshotPlugin","activity ==null");

            return;
        }
        ViewGroup viewGroup = (ViewGroup) activity.getWindow().getDecorView();
        View flag_secure = viewGroup.findViewWithTag("FLAG_SECURE");
        try {
            if (isSecure) {
                if (flag_secure == null) {
                    flag_secure = new View(activity);
                    flag_secure.setTag("FLAG_SECURE");
                    viewGroup.addView(flag_secure, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
                }
            } else {
                viewGroup.removeView(flag_secure);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        activity = binding.getActivity();
        Log.d("onAttachedToActivity","onAttachedToActivity");
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        activity = binding.getActivity();

    }

    @Override
    public void onDetachedFromActivity() {
        activity = null;
    }
}
